package com.nuotu.datawall.clouddata.control;

import android.content.Context;

import com.nuotu.datawall.clouddata.been.TokenBean;
import com.nuotu.datawall.clouddata.util.FinalStr;
import com.nuotu.datawall.clouddata.util.JsonUtil;
import com.nuotu.datawall.clouddata.util.PrefUtils;
import com.nuotu.datawall.clouddata.util.ServerConfig;
import com.nuotu.datawall.clouddata.util.Tool;

import org.xutils.common.Callback;
import org.xutils.common.TaskController;
import org.xutils.common.task.TaskControllerImpl;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.HashMap;
import java.util.Map;

public class Token {

    private static volatile Token instance;
    private static Context mContext;
    private TokenTodoListener tokenTodoListener;
    public static Token getInstance(Context context) {
        if (instance == null) {
            synchronized (Token.class) {
                if (instance == null) {
                    mContext=context;
                    instance = new Token();
                }
            }
        }
        return  instance;
    }
    public   void getToken(){
        RequestParams params = new RequestParams(ServerConfig.POST_TOKEN_URL_KEY);
        params.setReadTimeout(20000);
        Map map =new HashMap();
        String client_id =PrefUtils.getString(mContext,FinalStr.SERVER_LIBID_KEY,"ntlib");
        String client_secret =PrefUtils.getString(mContext,FinalStr.SERVER_LIBSECRET_KEY,"Nt.net123");
        String username =PrefUtils.getString(mContext,FinalStr.SERVER_USERNAME_KEY,"lib_admin");
        String password =PrefUtils.getString(mContext,FinalStr.SERVER_PASSWORD_KEY,"nuotu.net123");
        map.put("client_id",client_id);
        map.put("client_secret",client_secret);
        map.put("grant_type","password");
        map.put("username",username);
        map.put("password",password);
        params.setAsJsonContent(true);
        params.setBodyContent(JsonUtil.toJson(map));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                TokenBean tokenBean=JsonUtil.fromJson(result, TokenBean.class);
                String token=tokenBean.getToken_type()+" "+tokenBean.getAccess_token();
                PrefUtils.setString(mContext, FinalStr.SERVER_TOKEN,token);
                if (null != tokenTodoListener) {
                    tokenTodoListener.onGetTokenSuccess();
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                if (null != tokenTodoListener) {
                    tokenTodoListener.onGetTokenError();
                }
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });
    }
    public  void refreshToken(String result ){
        if(result.indexOf("invalid_token")>-1||(result.indexOf("unauthorized")>-1&& result.indexOf("Full authentication is required to access this resource")>-1)){
            getToken();
        }
    }
    public interface TokenTodoListener {
        void onGetTokenSuccess();
        void onGetTokenError();
    }
    public Token setTokenTodoListener(TokenTodoListener particleAnimListener) {
        tokenTodoListener = particleAnimListener;
        return instance;
    }
}
